﻿using System.Collections;
using UnityEngine;
namespace FrameWorkSongDemo {
    public class RadiationControlDemo : MonoBehaviour
    {
        public GameObject Plane;
        Ray my_Ray;
        RaycastHit my_Hit;
        Material my_Material;
        Coroutine my_Coroutine;
        void Start()
        {
            my_Material = Plane.GetComponent<MeshRenderer>().material;
        }
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                my_Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(my_Ray, out my_Hit))
                {

                    if (my_Coroutine!=null)
                    {
                        StopCoroutine(my_Coroutine);
                    }
                   // Plane.transform.position = my_Hit.point;
                    MatInitialSet(my_Material , new Vector4(my_Hit.point.x, 0, my_Hit.point.z, 0));
                    my_Coroutine= StartCoroutine(ChangeMat( my_Material, 1.5f));
                }
            }
        }
        void MatInitialSet(Material material, Vector4 centrePoint)
        {
            material.SetFloat("_Range", 0);
            material.SetVector("_CentorPoint", centrePoint);
        }
        void MatChangeSet(Material material, float Range)
        {
            material.SetFloat("_Range", Range);
        }
        IEnumerator ChangeMat( Material material, float time)
        {
            float Range = 0;
            while (Range < 1)
            {
                Range += Time.deltaTime / time;
                Range = Range > 1 ? 1 : Range;
                MatChangeSet(material, Range);
                yield return null;
            }
        }
    }
}
